using Tycho.Parser.Tokens;

namespace Tycho.Parser.Peg {
    public class ChoiceRule<Context> : Rule<Context> {
        private readonly Rule<Context>[] rules;

        public ChoiceRule (params Rule<Context>[] rules) {
            this.rules = rules;
        }

        protected override RuleParseResult<Context> ReallyParse (Token[] tokens, int index, Context context) {
            foreach (var rule in rules) {
                RuleParseResult<Context> result = rule.Parse (tokens, index, context);
                if (result != null) {
                    return result;
                }
            }

            return null;
        }
    }
}